package net.jueb.sniff4j.extr.filter.builder;
import java.io.IOException;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.UnpooledUnsafeDirectByteBuf;
import io.netty.channel.Channel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import net.jueb.sniff4j.extr.base.AbstractRuntime;
import net.jueb.sniff4j.extr.base.ServerContext;
import net.jueb.sniff4j.extr.base.UserContext;
import net.jueb.sniff4j.extr.util.BytesUtil;

/**
 * 抽象重建接口
 * 重建失败返回null则终止数据发送
 * @author Administrator
 */
public abstract class RBuilder{
	protected static final InternalLogger log = InternalLoggerFactory.getInstance(RBuilder.class);
	private AbstractRuntime runtime;
	private Channel fromChannel;
	private Channel toChannel;
	private ServerContext serverContext;
	private UserContext userContext;
	
	public RBuilder(AbstractRuntime runtime,Channel fromChannel, Channel toChannel) {
		this.runtime=runtime;
		this.serverContext=runtime.getServerConext();
		this.fromChannel=fromChannel;
		this.toChannel=toChannel;
		this.userContext=findUserContext();
	}
	private UserContext findUserContext()
	{
		UserContext ctx=null;
		ctx=this.runtime.getCurrentUserContext(fromChannel);
		if(ctx==null)
		{
			ctx=this.runtime.getCurrentUserContext(toChannel);
		}
		return ctx;
	}
	protected final Channel getFromChannel() {
		return fromChannel;
	}
	protected final Channel getToChannel() {
		return toChannel;
	}
	protected final AbstractRuntime getRuntime() {
		return runtime;
	}
	protected final ServerContext getServerContext() {
		return serverContext;
	}
	protected final UserContext getUserContext() {
		return userContext;
	}
	/**
	 * 是否是最终形态
	 * @return 如果为true则不进行后续重建
	 */
	public abstract boolean isFinallRebuild();
	/**
	 * 返回通过重建器的重建目标
	 * 默认直接返回不重建
	 * @return 如果为null则表示重建失败,则终止发送
	 */
	public Object cross(Object rbuildTarget)
	{
		return rbuildTarget;
	}
	/**
	 * 展示过滤目标
	 */
	protected  void showFiltTargetInfo(final Object filtTarget)
	{
		String userNmae=getUserContext().getUser().getName();
		String from=getFromChannel().remoteAddress().toString();
		String to=getToChannel().remoteAddress().toString();
		String info=userNmae+"{"+from+"=>"+to+"}:\n";
		if(filtTarget instanceof UnpooledUnsafeDirectByteBuf)
		{
			UnpooledUnsafeDirectByteBuf buf=(UnpooledUnsafeDirectByteBuf)filtTarget;
			log.debug("DefaultObjectFilter==>showFiltTargetInfo:+\n"+info+hexDump(buf));
			buf.resetReaderIndex();//复位读取位置
		}else
		{
			log.debug(info+filtTarget.toString());
		}
	}
	/**
	 * 返回十六进制字符串格式
	 * @param buf
	 * @return
	 */
	protected String hexDump(ByteBuf buf)
	{
		buf.markReaderIndex();//标记读取前的位置
		int length=buf.readableBytes();
		byte[] data=new byte[length];
		ByteBufInputStream bis=new ByteBufInputStream(buf, length);
		try {
			bis.readFully(data);//读取所有数据到data里面
			bis.close();
		} catch (IOException e) {
		}
		return length+"Bytes:\n"+BytesUtil.printBytes(data);
	}
	
}
